package com.hafu.view.GYS;

import com.hafu.model.GysInfo;
import com.hafu.service.GysInfoService;
import com.hafu.util.StringUtil;
import java.awt.Component;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.GroupLayout.Alignment;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.LayoutStyle;


public class addGysFrm extends JFrame {
    private GysInfoService gysInfoService = new GysInfoService();

    public addGysFrm() {
        this.initComponents();
    }

    private void button1(ActionEvent e) {
        String name = this.textField1.getText();
        String tel = this.textField2.getText();
        if (StringUtil.isEmpty(name)) {
            JOptionPane.showMessageDialog((Component)null, "老师名称不能为空");
        } else if (StringUtil.isEmpty(tel)) {
            JOptionPane.showMessageDialog((Component)null, "老师电话不能为空");
        } else if (!StringUtil.isPhoneNumber(tel)) {
            JOptionPane.showMessageDialog((Component)null, "电话号码格式不正确");
        } else {
            GysInfo gysInfo = new GysInfo(name, tel);
            ArrayList<GysInfo> gysInfoByName = this.gysInfoService.getGysInfoByName(gysInfo);
            if (gysInfoByName.size() > 0) {
                JOptionPane.showMessageDialog((Component)null, "老师信息已存在,请勿重复添加");
            } else {
                Boolean aBoolean = this.gysInfoService.addGysInfo(gysInfo);
                if (aBoolean) {
                    JOptionPane.showMessageDialog((Component)null, "添加成功");
                    this.dispose();
                } else {
                    JOptionPane.showMessageDialog((Component)null, "添加失败");
                }

            }
        }
    }
    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        // Generated using JFormDesigner Evaluation license - aaaa
        label1 = new JLabel();
        label2 = new JLabel();
        textField1 = new JTextField();
        textField2 = new JTextField();
        button1 = new JButton();

        //======== this ========
        setTitle("\u6dfb\u52a0\u8001\u5e08\u4fe1\u606f");
        var contentPane = getContentPane();

        //---- label1 ----
        label1.setText("\u8001\u5e08\u540d\u5b57");

        //---- label2 ----
        label2.setText("\u8001\u5e08\u7535\u8bdd");

        //---- button1 ----
        button1.setText("\u63d0\u4ea4");
        button1.addActionListener(e -> button1(e));

        GroupLayout contentPaneLayout = new GroupLayout(contentPane);
        contentPane.setLayout(contentPaneLayout);
        contentPaneLayout.setHorizontalGroup(
            contentPaneLayout.createParallelGroup()
                .addGroup(contentPaneLayout.createSequentialGroup()
                    .addGap(121, 121, 121)
                    .addGroup(contentPaneLayout.createParallelGroup()
                        .addComponent(label1, GroupLayout.PREFERRED_SIZE, 76, GroupLayout.PREFERRED_SIZE)
                        .addComponent(label2, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, 124, Short.MAX_VALUE)
                    .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.TRAILING)
                        .addComponent(textField1, GroupLayout.PREFERRED_SIZE, 284, GroupLayout.PREFERRED_SIZE)
                        .addComponent(textField2, GroupLayout.PREFERRED_SIZE, 284, GroupLayout.PREFERRED_SIZE))
                    .addContainerGap(288, Short.MAX_VALUE))
                .addGroup(contentPaneLayout.createSequentialGroup()
                    .addGap(307, 307, 307)
                    .addComponent(button1, GroupLayout.PREFERRED_SIZE, 147, GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(439, Short.MAX_VALUE))
        );
        contentPaneLayout.setVerticalGroup(
            contentPaneLayout.createParallelGroup()
                .addGroup(contentPaneLayout.createSequentialGroup()
                    .addGap(135, 135, 135)
                    .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                        .addComponent(label1, GroupLayout.PREFERRED_SIZE, 40, GroupLayout.PREFERRED_SIZE)
                        .addComponent(textField1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                    .addGap(33, 33, 33)
                    .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                        .addComponent(label2, GroupLayout.PREFERRED_SIZE, 40, GroupLayout.PREFERRED_SIZE)
                        .addComponent(textField2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                    .addGap(143, 143, 143)
                    .addComponent(button1, GroupLayout.PREFERRED_SIZE, 43, GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(175, Short.MAX_VALUE))
        );
        pack();
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    // Generated using JFormDesigner Evaluation license - aaaa
    private JLabel label1;
    private JLabel label2;
    private JTextField textField1;
    private JTextField textField2;
    private JButton button1;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
}
